/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dataBasePlaylisted;

import controlPlaylisted.ControlPlaylistCommand;
import Request.RequestPlaylist;
import Response.ResponsePlaylist;

import dataPlaylisted.Playlist;

import java.beans.XMLEncoder;

import java.io.IOException;

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;

import java.util.ArrayList;

/**
 *
 * @author Administrador
 */
public class setPlaylistListCommand implements ControlPlaylistCommand{

    public void execute(RequestPlaylist _req, ResponsePlaylist _resp) {

        String file_name = "PlaylistModel.xml";
        FileOutputStream m_fos = null;
        ObjectOutputStream m_out = null;
        XMLEncoder m_xenc;

        try{
            m_fos = new FileOutputStream(file_name);
        }catch(IOException e){
        }

//        try{
//            m_out = new ObjectOutputStream(m_fos);
//        }catch(IOException e){
//        }

        m_xenc = new XMLEncoder(m_fos);

        ArrayList<Playlist> list;

        list = _req.getObjectList();

        ArrayList<String> str_list;
        str_list = new ArrayList<String>();

        for (int i=0; i < list.size(); i++){
            str_list.add(list.get(i).toString(list.get(i).getId()));
            str_list.add(list.get(i).toString(list.get(i).getUserId()));
            str_list.add(list.get(i).toString(list.get(i).getMusicNum()));
            for (int j=0; j < list.get(i).getPlaylist().size(); j++){
                str_list.add(list.get(i).getPlaylist().get(j).getTrackName());
                str_list.add(list.get(i).getPlaylist().get(j).getArtistName());
                str_list.add(list.get(i).getPlaylist().get(j).getSyle());
                str_list.add(list.get(i).getPlaylist().get(j).getCammelot());
            }
        }

        // Write object.
	m_xenc.writeObject(str_list);
	m_xenc.flush();
	m_xenc.close();
	try{
            m_fos.close();
        }catch(IOException e){
        }
    }
}
